package com.android.djlmapp.activity.home.web;

import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.android.djlmapp.R;
import com.android.djlmapp.activity.home.web.base.BaseWebActivity;
import com.android.djlmapp.activity.order.OrderListActivity;
import com.android.djlmapp.http.ComPareUtils;
import com.android.djlmapp.token.GetUrlStr;
import com.android.djlmapp.tools.global.UserInfoUtils;

import butterknife.BindView;

import static com.android.djlmapp.http.GlobalUrls.RECHARGE_ABOUT_URL;

/**
 * @Author wangziheng
 * @ProjectName djlm
 * @ClassName RechargeActivity
 * @package com.android.djlmapp.activity.home.web
 * @data 2019/1/11 12:50
 * @Describe QQ、流量、游戏充值
 * @Change
 */
public class RechargeActivity extends BaseWebActivity {


    @BindView(R.id.tv_top_jump)
    TextView mRechargeAbortTv;

    @Override
    protected void initView() {
        setWebViewClient(mWebViewClient);
        mRechargeAbortTv.setText("返利说明");
        mRechargeAbortTv.setVisibility(View.VISIBLE);
        super.initView();
        mRechargeAbortTv.setOnClickListener(v -> {
            String token = UserInfoUtils.getUserInfoUtilsInstance().getToken();
            String version = UserInfoUtils.getUserInfoUtilsInstance().getVersion();
            String sign = new ComPareUtils.ComPareBuilder()
                    .put("token", token)
                    .put("version", version)
                    .create()
                    .getSignString();
            String requestRechargeAboutUrl = RECHARGE_ABOUT_URL + "?token=" + token + "&version=" + version + "&sign=" + sign;
            Intent intentRechargeAbout = new Intent(RechargeActivity.this, BaseWebActivity.class);
            intentRechargeAbout.putExtra(PAGE_URL_KEY, requestRechargeAboutUrl);
            startActivity(intentRechargeAbout);
        });
    }

    public WebViewClient mWebViewClient = new WebViewClient() {
        /**
         * 充值功能特有
         * @param view
         * @param request
         * @return
         */
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                Uri uri = request.getUrl();
                String strUrl = uri.getPath();
                Log.i("充值", "shouldOverrideUrlLoading: " + strUrl);
                // 刷新token
                if (strUrl.contains(TOKEN_DEAD_URL)) {
                    GetUrlStr.getStrUrl(RechargeActivity.this);
                    return true;
                } else if (strUrl.contains(TO_ORDER_LIST_URL)) {
                    // 订单页
                    isUseFinishAnimation = false;
                    startActivity(new Intent(RechargeActivity.this, OrderListActivity.class));
                    finish();
                    return true;
                } else if (strUrl.contains(TO_FORGET_PSW_URL)) {
                    // 忘记密码
                    startActivity(new Intent(RechargeActivity.this, ForgetPwdActivity.class));
                    return true;
                }
//                else {
//                    Intent intent = new Intent();
//                    intent.setClass(RechargeActivity.this, RechargeActivity.class);
//                    intent.putExtra(PAGE_URL_KEY, MAIN_URL + strUrl);
//                    startActivity(intent);
//                    return true;
//                }
            }
            return super.shouldOverrideUrlLoading(view, request);
        }

        /**
         * 充值功能特有
         * @param view
         * @param url
         * @return
         */
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Log.i("充值", "shouldOverrideUrlLoading: " + url);
            // 刷新token
            if (url.contains(TOKEN_DEAD_URL)) {
                GetUrlStr.getStrUrl(RechargeActivity.this);
                return true;
            } else if (url.contains(TO_ORDER_LIST_URL)) {
                // 订单页
                isUseFinishAnimation = false;
                startActivity(new Intent(RechargeActivity.this, OrderListActivity.class));
                finish();
                return true;
            } else if (url.contains(TO_FORGET_PSW_URL)) {
                // 忘记密码
                startActivity(new Intent(RechargeActivity.this,
                        ForgetPwdActivity.class));
                return true;
            } else {
//                Intent intent = new Intent();
//                intent.setClass(RechargeActivity.this, RechargeActivity.class);
//                intent.putExtra(PAGE_URL_KEY, url);
//                startActivity(intent);
                return false;
            }
        }
    };

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void btnBack() {
        finish();
    }
}
